package com.sfx.算法专题.链表.公共结点;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-07-29
 * Time: 15:13
 */
public class Code_1_getIntersectionNode {
    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }
    static public class Solution {
        public  ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            ListNode curA = headA;
            ListNode curB = headB;
            while(curA!=curB){
                curA = curA==null ? headB : curA.next;
                curB = curB == null ? headA : curB.next;
            }
            return curA;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        ListNode node2 = new ListNode(2);
        ListNode node6 = new ListNode(6);
        ListNode node4 = new ListNode(4);
        ListNode node1 = new ListNode(1);
        ListNode node5 = new ListNode(5);
        node2.next = node6;
        node6.next = node4;
        node1.next = node5;
        solution.getIntersectionNode(node2,node1);
    }
}